#!/bin/usr/python3

# 加载库
from itchat.content import *
import requests
import json
import itchat
import time

# 调用图灵机器人的api
def get_response(msg,sign):
    api = 'http://openapi.turingapi.com/openapi/api/v2'
    #
    dat = {
        "perception": {
            "inputText":{
                "text": msg
            },
            "inputImage":{
                "url": "imageUrl"
            },
            "selfInfo": {
                "location":{
                    "city": "北京",
                    "province": "北京",
                    "street": "阜外大街"
                }
            }
        },
        "userInfo":{
            "apiKey": "087c6eb643ce4781a8fad02cb6e5f571", #api_key
            "userId": "fool"
        }
    }
    dat = json.dumps(dat)
    r = requests.post(api,data=dat).json()

    mesage = r['results'][0]['values']['text']
    print(r['results'][0]['values']['text'])

    if sign == 'user':
        send_msg(mesage,'太阳当空赵')
    elif sign == 'group':
        send_group(mesage,'rebort测试')
    
    return mesage


#发送消息给特定好友
def send_msg(mesage,user_name):
    friends_list = itchat.get_friends(update=True)
    users = itchat.search_friends(name=u'%s' % user_name)
    userName = users[0]["UserName"]
    itchat.send(mesage,toUserName=userName)

#发送消息给指定群
def send_group(mesage,group_name):
    group_list = itchat.get_chatrooms(update=True)
    groups = itchat.search_chatrooms(name=u'%s' % group_name)
    GroupName = groups[0]["UserName"]
    itchat.send(mesage,toUserName = GroupName)

#接受微信信息(只接受用户，群消息不接收)
@itchat.msg_register(itchat.content.TEXT)
def _(msg):
    """
    user = msg['User'] #用户信息
    user_name = user['NickName'] #用户名
    user_com = user['RemarkName'] #备注名
    
    """
    user = msg['User'] #用户信息
    user_com = user['RemarkName'] #备注名
    print(user_com+'好友说:',msg.text)
    info_msg = msg.text
    get_response(info_msg,'user')

#接受微信群信息
@itchat.msg_register(TEXT,isGroupChat=True) # isGroupChat=True 检测群组消息
def get_group_msg(msg):
    group_info = msg.User #NickName
    group_name = group_info.NickName
    print(group_name+'群组:',msg.Text)
    info_msg = msg.Text
    get_response(info_msg,'group')



#file下载
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
    msg.download(msg.fileName)
    typeSymbol = {
        PICTURE: 'img',
        VIDEO: 'vid',
    }.get(msg.type,'fil')
    return '@%s@%s' % (typeSymbol,msg.fileName)


#定时任务
def contral_job():
    while True:
        now_time = time.strftime("%H:%M:%S" , time.localtime()) # 不断刷新 
        #print(now_time)
        if now_time  == '14:04:00':
            time.sleep(1) #睡眠一秒,使其不会输出多次
            hint_word = "呦呦"
            send_msg(hint_word,'W。')

if __name__ == '__main__':
    itchat.auto_login(hotReload=True)#hotReload=True 可暂存登录状态，退出后一定时间内重启不用再次扫码登录
    time.sleep(5)
    
    #contral_job()

    #保持运行
    itchat.run()



